﻿namespace Centido.Core.Test
{
    using System;
    using System.Data.Common;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Testing text columns
    /// </summary>
    [TestClass]
    public class TextColumnText : ColumnBaseTest<string>
    {
        /// <summary>
        /// Text table column
        /// </summary>
        private TextColumn Column
        {
            get { return GenericColumn as TextColumn; }
        }

        /// <summary>
        /// Get column type
        /// </summary>
        /// <returns></returns>
        protected override ColumnType GetColumnType()
        {
            return ColumnType.Text;
        }

        /// <summary>
        /// Get large value
        /// </summary>
        /// <returns></returns>
        protected override string GetLargeValue()
        {
            var str = new StringBuilder();

            for (var i = 0; i < 100; i++)
            {
                str.Append(Guid.NewGuid());
            }

            return str.ToString();
        }

        /// <summary>
        /// Get small value
        /// </summary>
        /// <returns></returns>
        protected override string GetSmallValue()
        {
            return "Text value";
        }

        /// <summary>
        /// Convert db value
        /// </summary>
        /// <param name="dbValue">Db value</param>
        /// <returns></returns>
        protected override string ConvertDbValue(object dbValue)
        {
            return DbValue.GetString(dbValue);
        }

        /// <summary>
        /// Inserting large value
        /// </summary>
        [TestMethod]
        public void InsertingLargeValue()
        {
            var random = new Random(Environment.TickCount);

            var str = new StringBuilder();

            for (var i = 0; i < 1024*1024; i++)
            {
                str.Append(random.Next()%10);
            }

            //Inserting the value
            var rowId = Table.InsertRow(GenericColumn.Value(str.ToString()));

            Assert.AreEqual(1, Table.RowCount);

            //Reading inserted value
            var inserted = GenericColumn.EmptyValue();

            Table.ReadRow(rowId, inserted);

            Assert.IsNotNull(inserted.Value);
            Assert.AreEqual(inserted.GetString().Length, str.Length);
            Assert.AreEqual(inserted.GetString(), str.ToString());
        }

        /// <summary>
        /// Inserting null value
        /// </summary>
        [TestMethod]
        public void InsertingNullValue()
        {
            //Inserting null value
            var rowId = Table.InsertRow(GenericColumn.Value(null));

            Assert.AreEqual(Table.RowCount, 1);

            Assert.IsNull(GenericColumn.Read(rowId).GetString());
        }


        /// <summary>
        /// Updating large value
        /// </summary>
        [TestMethod]
        public void UpdatingLargeValue()
        {
            var random = new Random(Environment.TickCount);

            var str = new StringBuilder();

            for (var i = 0; i < 500*1024; i++)
            {
                str.Append(random.Next()%10);
            }

            //Inserting the row
            var rowId = Table.InsertRow();

            Assert.AreEqual(1, Table.RowCount);

            //Updating the row
            Table.UpdateRow(rowId, Column.Value(str.ToString()));

            //Reading updated value
            var updated = GenericColumn.EmptyValue();

            Table.ReadRow(rowId, updated);

            Assert.IsNotNull(updated.Value);
            Assert.AreEqual(updated.GetString().Length, str.Length);
            Assert.AreEqual(updated.GetString(), str.ToString());
        }

        /// <summary>
        /// Updating column to null
        /// </summary>
        [TestMethod]
        public void UpdatingToNull()
        {
            //Inserting some value
            var rowId = Table.InsertRow(GenericColumn.Value("aaa"));

            Assert.AreEqual(Table.RowCount, 1);

            Assert.IsNotNull(GenericColumn.Read(rowId).GetString());

            //Updating to null
            Table.UpdateRow(rowId, GenericColumn.Value(null));

            Assert.IsNull(GenericColumn.Read(rowId).GetString());
        }
    }
}